<?php

namespace App\Models\OpenShop\Shop;

use App\Models\BaseModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class OpenShopHandleExpireShopHistoryModel extends BaseModel
{
    use HasFactory;

    const TYPE_0 = 0;
    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;
    const TYPE_4 = 4;
    const TYPE_5 = 5;

    const TYPE_TEXT_DAY = [
        self::TYPE_1 => "距离到期90天消息推送",
        self::TYPE_2 => "距离到期60天消息推送",
        self::TYPE_3 => "距离到期30天消息推送",
        self::TYPE_4 => "到期当天消息推送",
        self::TYPE_5 => "到期次日消息推送",
    ];

    const TYPE_TEXT_GROUP = [
        self::TYPE_0 => "全部",
        self::TYPE_1 => "近90天",
        self::TYPE_2 => "近60天",
        self::TYPE_3 => "近30天",
        self::TYPE_4 => "当天到期",
        self::TYPE_5 => "已过期",
    ];

    protected $table = "open_shop_handle_expire_shop_history";

    protected $fillable = [
        "mainId",
        "type",
        "createdAt",
    ];


    /**
     * @param OpenShopUserContractModel $userContractModel
     * @return int
     */
    public static function getType(OpenShopUserContractModel $userContractModel): int
    {
        if ($userContractModel->mainProcessRel) {
            $remainingSeconds = $userContractModel->endTime - strtotime(formatDateYmd(time()));
            $remainingDays    = (int)($remainingSeconds / (24 * 60 * 60));

            if (date("Ymd", time()) == date("Ymd", $userContractModel->endTime)) {
                return self::TYPE_4;
            }

            if (
                date("Ymd", time())
                ==
                date("Ymd", strtotime("+1 day", $userContractModel->endTime))
            ) {
                return self::TYPE_5;
            }

            if ($remainingDays == 30) {
                return self::TYPE_3;
            }

            if ($remainingDays == 60) {
                return self::TYPE_2;
            }

            if ($remainingDays == 90) {
                return self::TYPE_1;
            }
        }

        return 0;
    }
}
